<?php

namespace backend\modules\home\models;

use Yii;

/**
 * This is the model class for table "mh_booklist".
 *
 * @property int $id
 * @property int $cid
 * @property int $parent_id
 * @property int $type 1单行本 2多行本
 * @property int $number
 * @property string $front_title
 * @property string $backend_title
 * @property string $thumb_img
 * @property int $cont_num
 * @property int $click_num
 * @property int $zan_num
 * @property string $intro
 * @property int $is_recommend 是否推荐
 * @property int $is_hot 是否热门
 * @property int $is_over 0连载中 1 完结
 * @property int $is_advert 0有广告 1无广告
 * @property int $is_free 0免费 1付费
 * @property string $mark
 * @property string $remark 备注
 * @property string $date
 * @property int $sort
 * @property int $status
 * @property int $create_at
 * @property int $update_at
 */
class Booklist extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'mh_booklist';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['cid', 'parent_id', 'number', 'front_title', 'backend_title', 'date'], 'required'],
            [['cid', 'parent_id', 'number', 'cont_num', 'click_num', 'zan_num', 'create_at', 'update_at', 'sort'], 'integer'],
            [['type', 'is_recommend', 'is_hot', 'is_over', 'is_advert', 'is_free', 'status'], 'integer'],
            [['front_title', 'backend_title'], 'string', 'max' => 50],
            [['thumb_img', 'intro', 'remark'], 'string', 'max' => 255],
            [['mark', 'date'], 'string', 'max' => 20],
            ['number', 'unique', 'targetClass' => self::className(), 'message' => 'number已存在,请刷新'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'cid' => '类别',
            'parent_id' => '父目录',
            'type' => '类型',
            'number' => '编号',
            'front_title' => '前台标题',
            'backend_title' => '后台标题',
            'thumb_img' => '封面图',
            'cont_num' => '集数',
            'click_num' => '点击量',
            'zan_num' => '点赞数',
            'intro' => '简介',
            'is_recommend' => '是否推荐',
            'is_hot' => '是否热门',
            'is_over' => '是否完结',
            'is_advert' => '是否放映广告',
            'is_free' => '是否付费',
            'mark' => '标签',
            'remark' => '备注',
            'sort' => '排序',
            'date' => '发布日期',
            'status' => '状态',
            'create_at' => '创建时间',
            'update_at' => '更新时间',
        ];
    }

    public function getPhotolist(){
        return BooklistImg::find()
            ->where(['number_code' => $this->number])
            ->orderBy([
                'sort' => SORT_DESC,
                'title' => SORT_ASC
            ])
            ->asArray()->all();
    }

    public function getLastBook(){
        return Booklist::find()
            ->where(['parent_id' => $this->id, 'status' => 1])
            ->orderBy('id desc')
            ->one();
    }

    public function beforeSave($insert)
    {
        if(parent::beforeSave($insert)){
            if($this->isNewRecord){
                $this->create_at = time();
            }
            $this->update_at = time();
            return true;
        }
        return false;
    }
}
